from ConfigParser import ConfigParser
import os

# path to config
phonikdir = os.path.expanduser('~') + os.sep + '.config' + os.sep + 'phonik'

# create config dirs
if not os.path.exists(phonikdir):
    os.makedirs(phonikdir)

# set config file
configpath = phonikdir + os.sep + 'settings.ini'
configParser = ConfigParser()


# if config exists - read it, else create app section
configExists = False
if os.path.exists(configpath):
    configParser.read(configpath)
    configExists = True
else:
    configParser.add_section('app')


def save():
    with open(configpath, 'wb') as configfile:
        configParser.write(configfile)


def set(key, value, name='app'):
    configParser.set(name, key, value)


def get(key, default=None, name='app'):
    if hasOption(key, name):
        return configParser.get(name, key)
    else:
        return default


def hasOption(key, name='app'):
    return configParser.has_option(name, key)

def hasSection(name):
    return configParser.has_section(name)


def addSection(name):
    configParser.add_section(name)

def configCreated():
    return configExists

